/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package testCase;

import java.io.*;
import java.util.Vector;

import org.junit.Test;
import junit.framework.TestCase;
import csBakery.*;

/*
 * 	@release Final Release
 *	@author  Leung Ho Yan
 *	@version 5.1.3
 *
 * 	@release Release 2
 *	@author  Yip Tak Wai
 *	@version 4.0.1
 *
 * 	@release Release 1
 *	@author  Lee Cheuk Wang
 *	@version 1.0.0
 */

public class StockControllerTest extends TestCase {
	private StockController sc;
	private Stock s;
	private LogController log;
	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private final String stockPath = "test\\testStock.txt";
	private String separator;
	
	public void setUp()	{	
		setUpStockData();
		sc = new StockController(stockPath);
		s = sc.getStocks();
		//Set up for output stream testing
		System.setOut(new PrintStream(outContent));
		separator = System.getProperty("line.separator");
		//Set up LogControllerStub 
		class LogStub extends LogController{}
		log = new LogStub();
	}

	private void setUpStockData() {
		//set up initial stock
		String str1 = "Bagel 80\n";
		String str2 = "Baguette 70\n";
		String str3 = "Barbari Bread 70\n";
		
		byte[] myBytes1 = str1.getBytes();
		byte[] myBytes2 = str2.getBytes();
		byte[] myBytes3 = str3.getBytes();
		
		try {
		File logFile = new File(stockPath);
		if (!logFile.exists()){
			logFile.createNewFile();
		}
		FileOutputStream fileStream = new FileOutputStream(logFile, false);
		fileStream.write(myBytes1);
		fileStream.write(myBytes2);
		fileStream.write(myBytes3);
		fileStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void tearDown()	{
		System.setOut(null);
		
		PrintWriter writer;
		try {
			writer = new PrintWriter(stockPath);
		} catch (FileNotFoundException e) {
			writer = null;
		}
		if (writer!=null){
			writer.print("");
			writer.close();
		}
	}
	
	@Test
	//Update Stock Test case	(80 + 30) for Bagel
	public void testupdateStockCase(){
		sc.updateStock(log,"Bagel",30);
		assertEquals(110,s.getProductQuantity("Bagel"));
		assertEquals(70,s.getProductQuantity("Baguette"));
	}
	
	@Test
	//Update Sale Test case 1	(80 - 60) for Bagel
	public void testupdateSaleCase1(){
		assertTrue(sc.updateSale(log,"Bagel",60));
		assertEquals(20,s.getProductQuantity("Bagel"));
		assertEquals(70,s.getProductQuantity("Baguette"));
	}
	
	@Test
	//Update Sale Test case 2	(80 - 150) for Bagel failed
	public void testupdateSaleCase2(){
		assertFalse(sc.updateSale(log,"Bagel",150));
		assertEquals(80,s.getProductQuantity("Bagel"));
		assertEquals(70,s.getProductQuantity("Baguette"));
	}
	
	@Test
	//Output Stream Test case 1
	public void testStockStateCase1(){
		sc.updateStock(log,"Bagel",30);
		sc.updateSale(log,"Baguette",40);
		String state = "-----Stock State-----" + separator + "Current Stock: " + separator + "Bagel 110\nBaguette 30\nBarbari Bread 70\n" + separator;
		sc.stockState();
	    assertEquals(state, outContent.toString());
	}
	
	//get the product list successfully
	@Test 
	public void testGetProductList(){
		Vector<String> vector = new Vector<String>(2);
		vector.add("Bagel");
		vector.add("Baguette");
		vector.add("Barbari Bread");
		assertEquals(vector, sc.getStockProductList());
	}
	
	@Test
	public void testSaveLoadRecord(){
		assertTrue(sc.updateSale(log,"Bagel",60));
		assertEquals(20,s.getProductQuantity("Bagel"));
		assertEquals(70,s.getProductQuantity("Baguette"));
		
		sc.saveStockRecord();
		
		assertTrue(sc.updateSale(log,"Baguette",60));
		assertEquals(20,s.getProductQuantity("Bagel"));
		assertEquals(10,s.getProductQuantity("Baguette"));
		
		sc.loadStockRecord();
		
		assertEquals(20,s.getProductQuantity("Bagel"));
		assertEquals(70,s.getProductQuantity("Baguette"));
	}
}

